package com.skyworth.web.admin.category.repository;


import com.skyworth.web.admin.category.domain.CategoryModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by lxs on 2016/8/8.
 */
public interface CategoryRepository extends PagingAndSortingRepository<CategoryModel, Long> {

	@Query("select category from CategoryModel category where category.removeFlag=0")
	Page<CategoryModel> allValidCategory(Pageable pageable);

	@Query("select category from CategoryModel category where category.storeId=?1 and category.removeFlag=0")
	Page<CategoryModel> getCategoryByStoreId(long storeId, Pageable pageable);

	@Query("select category from CategoryModel category where  category.storeId=?1 and category.removeFlag=0")
	List<CategoryModel> findByStoreId(long store_id);

	@Query("SELECT CASE WHEN COUNT(category) > 0 THEN 'true' ELSE 'false' END FROM CategoryModel category WHERE category.name = ?1 and category.storeId=?2 and category.removeFlag=0")
	boolean existByName(String name, long store_id);

	@Transactional
	@Modifying
	@Query("update CategoryModel m set m.removeFlag = 1 where m.id = ?1")
	public void del(long id);

	@Query("SELECT CASE WHEN COUNT(category) > 0 THEN 'true' ELSE 'false' END FROM CategoryModel category WHERE category.name = ?1 and category.storeId=?3 and category.id<>?2  and category.removeFlag=0")
	boolean existCategory(String name, long id, long store_id);
}
